<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size: 20px;">{{ user.username }}个人信息</span>

  </div>
  <!-- 用户信息卡片 -->
  <el-card style="margin: 20px;height: 100vh;">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="姓名">
            <el-input v-model="saveUserForm.nickname"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="性别">
            <el-radio-group v-model="saveUserForm.gender">
              <el-radio value="1" border style="margin:0;">男</el-radio>
              <el-radio value="0" border>女</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="年龄">
            <el-input v-model="saveUserForm.age"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="身份证号">
            <el-input v-model="saveUserForm.identity"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="手机号">
            <el-input v-model="saveUserForm.phone"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="医生姓名">
            <el-input v-model="saveUserForm.doctorName" :disabled="saveUserForm.role=10"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <template #footer>
      <el-tooltip effect="dark" content="填写好要修改的信息后点击" placement="top">
        <el-button type="primary" style="float: right;margin-left: 10px" @click="editUser()">修改信息</el-button>
      </el-tooltip>

      <el-tooltip effect="dark" content="点击弹出修改密码弹窗" placement="top">
        <el-button type="primary" style="float: right" @click="dialogVisible=true">修改密码</el-button>
      </el-tooltip>
    </template>
  </el-card>

  <!--修改密码弹窗-->
  <el-dialog :title="dialogTitle" v-model="dialogVisible" :before-close="handleClose"
             style="width:1000px;padding:40px;">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="50">
        <el-col :span="12">
          <el-form-item label="用户姓名">
            <el-input placeholder="请输入用户姓名" v-model="saveUserForm.nickname"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="用户密码">
            <el-input placeholder="请输入要修改的新密码" v-model="saveUserForm.password"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button @click="resetPassword" type="primary">保存</el-button>
    </template>
  </el-dialog>
</template>
<script setup>
//获取登录的用户信息
import {onMounted, ref} from "vue";
import axios from "axios";
import qs from "qs";
import {ElMessage} from "element-plus";


//一进页面加载
onMounted(() => {
  loadUser();
});


//获取当前登录用户的信息
const loadUser = () => {

  //有条件则根据条件查牙况数据,没有条件默认查所有牙况数据
  let data = qs.stringify(saveUserForm.value);
  console.log(data);
  axios.get(BASE_URL + '/v1/user/select?' + data).then((response) => {
    if (response.data.code == 2000) {
      console.log(response.data.data);
      saveUserForm.value = response.data.data;
    } else {
      ElMessage.error(response.data.msg);
    }
  })

};

//定义变量控制弹窗是否显示
const dialogVisible = ref(false);

//定义变量控制弹窗标题
const dialogTitle = ref('修改密码');

//实现关闭功能
const handleClose = () => {
  if (confirm('是否关闭本窗口?')) {
    //用户确认关闭后,将弹窗关闭且将表单数据清空
    dialogVisible.value = false;
  }
};

//获取当前登录用户的信息
const user = JSON.parse(localStorage.getItem('user'));
console.log(user);


//准备对象用来修该用户的表单信息
const saveUserForm = ref({
  id: user.id,
  username: user.username,
  nickname: '',
  password: user.password,
  gender: '1',
  age: '',
  identity: '',
  phone: '',
  doctorId: '',
  doctorName: '',
  role: '',
  status: ''
});


//修改用户信息
const editUser = () => {
  let data = qs.stringify(saveUserForm.value);
  axios.get(BASE_URL + '/v1/user/update?' + data).then((response) => {
    if (response.data.code == 2000) {
      ElMessage.success('修改成功!')
    } else {
      ElMessage.error(response.data.msg)
    }
  })
};

//修改密码
const resetPassword = () => {
  let data = qs.stringify(saveUserForm.value);
  axios.get(BASE_URL + '/v1/user/update/password?' + data)
      .then((response) => {
        if (response.data.code == 2000) {
          ElMessage.success('重置成功!')
        } else {
          ElMessage.error(response.data.msg)
        }
      })
};


</script>

<style scoped>

</style>